package com.voicenews.activity;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.voicenews.VoiceNewsApplication;
import com.voicenews.R;
import com.voicenews.playtts.ShowExtractedArticle;
import com.voicenews.utils.Utils;
import com.voicenews.webserviceconnect.ICallbackHandler;
import com.voicenews.webserviceconnect.WebServiceConnector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchArticlesByTagActivity extends DashBoardActivity implements ICallbackHandler {
	private EditText searchByTagEditText;
	private ListView articlesListView;
	private ImageButton searchButton;
	private ListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		String tag = "";
		if (intent != null) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				tag = intent.getExtras().getString("tag");
			}
		}
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_by_tag);
        setHeader(getString(R.string.SearchNewsByTagActivityTitle), true, true, false, false);
		searchByTagEditText = (EditText) findViewById(R.id.search_by_tag_edit_text);
		searchByTagEditText.getText().append(tag);
		articlesListView = (ListView) findViewById(R.id.search_by_tag_article_list);
		searchButton = (ImageButton) findViewById(R.id.search_button);

		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				searchByTag(searchByTagEditText.getText().toString());

			}
		});

		articlesListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				JSONObject json = (JSONObject) adapter.getItem(arg2);
				try {
					URL url = (URL) json.get("link");
					// Intent browserIntent = new
					// Intent(Intent.ACTION_VIEW,
					// Uri.parse(url.toString()));
					// startActivity(browserIntent);
					Intent i1 = new Intent(getApplicationContext(),
							ShowExtractedArticle.class);
					i1.putExtra("urlString", url.toString());
					i1.putExtra("title", json.get("title").toString());
					startActivity(i1);

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	private void searchByTag(String searchString) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(
				"idUser",
				this.getSharedPreferences(VoiceNewsApplication.filePrefrences,
						MODE_PRIVATE).getString("id", "noid"));
		params.put("searchString", searchString);
		callWebService(params);

	}

	private void callWebService(Map<String, String> params) {
		WebServiceConnector webServiceConnector = new WebServiceConnector(
				VoiceNewsApplication.hostService + "rest/services/",
				"articlesbytag", params, this, WebServiceConnector.GET_METHOD, this);
		webServiceConnector.execute();
	}

	@Override
	public void response(String response) {
		// TODO Auto-generated method stub
		System.out.println(response);
		adapter = new ArticlesListAdapter(this, Utils.convertToJSONObjects(response));
		articlesListView.setAdapter(adapter);
	}

}
